<?php

/**
 * @brief   商品模块控制器层
 * @author  huangfeng
 * @version 1.0
 * @since   2024-6-28
 */

namespace app\admin\controller;

use app\facade\Basic;
use think\Request;
use app\admin\service\Product as ProductService;
use app\admin\validate\Product as ProductValidate;

class Product extends BaseController
{
    /**
     * 获取商品列表
     * @param Request $request
     * @param ProductService $ProductService
     * @param ProductValidate $validate
     * @return \think\response\Json
     */
    public function getList(Request $request, ProductService $ProductService, ProductValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['cname'] = $request->param('cname', null);
        $param['style_code'] = $request->param('style_code', null);
        $param['sku'] = $request->param('sku', null);
        $param['code'] = $request->param('code', null);
        $param['category'] = $request->param('category', null);
        $param['brand'] = $request->param('brand', null);
        $param['start_date'] = $request->param('start_date', null);
        $param['end_date'] = $request->param('end_date', null);
        $param['billing_weight'] = $request->param('billing_weight', null);
        $param['model_id'] = $request->param('model_id', null);
        $param['status'] = $request->param('status', null);

        $param['abbr'] = $request->param('abbr', null); // 缩写
        $param['algorithm'] = $request->param('algorithm', null); // 算法
        $param['model'] = $request->param('model', null); // 型号
        $param['type'] = $request->param('type', null); // 类型
        $param['is_merge_product'] = $request->param('is_merge_product', null); // 是否关联聚合商品
        $param['is_have_inventory'] = $request->param('is_have_inventory', null); // 是否有库存


        $intPage = $request->param('pageIndex', 1);
        $intPageSize = $request->param('pageSize', 50);
        $arrData = $ProductService->getList($param, (int)$intPage, (int)$intPageSize);
        return json($arrData);
    }

    /**
     * @param Request $request
     * @param ProductService $ProductService
     * @param ProductValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request, ProductService $ProductService, ProductValidate $validate)
    {

        $param = $request->param();
        if (!$validate->scene('create')->check($param)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $arrData = $ProductService->create($param);
        return json($arrData);
    }

    /**
     * @param Request $request
     * @param ProductService $ProductService
     * @param ProductValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function exportPrice(Request $request, ProductService $ProductService)
    {
        $arrData = $ProductService->exportProduct();
        return json($arrData);
    }
    /**
     * @describe
     * @author: Bobo
     * @date: 2025-04-08 16:29:32
     * @param [type] @required
     * @return Renderable
     */
    public function update(Request $request, ProductService $ProductService, ProductValidate $validate)
    {

        $param = $request->param();
        if (!$validate->scene('update')->check($param)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['operator_id'] = $request->operator_id;
        $condation['id'] = $param['id'];
        unset($param['id']);
        $arrData = $ProductService->update($condation, $param);
        return json($arrData);
    }

    /**
     * 商品详情
     * @param Request $request 商品ID
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-6-28
     */
    public function getInfo(Request $request, ProductService $ProductService, ProductValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('show')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id', null);
        $arrData = $ProductService->getInfo($param);
        return json($arrData);
    }


    /**
     * 删除单条或多条数据
     * @param Request $request
     * @param ProductValidate $validate
     * @param ProductService $ProductService
     * @return \think\response\Json
     */
    public function delete(Request $request, ProductValidate $validate, ProductService $ProductService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids', null);
        $arrData = $ProductService->delete($param);
        return json($arrData);
    }
    /**
     * 筛选是否电源线
     * @param Request $request
     * @param ProductValidate $validate
     * @param ProductService $ProductService
     * @return \think\response\Json
     */
    public function hasPowerline(Request $request, ProductValidate $validate, ProductService $ProductService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids', null);
        $arrData = $ProductService->hasPowerline($param);
        return json($arrData);
    }

    /**
     * 商品库存列表
     * @param Request $request
     * @param ProductValidate $validate
     * @param ProductService $ProductService
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInventoryList(Request $request, ProductValidate $validate, ProductService $ProductService)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['name'] = $request->param('name', null);

        $intPage = $request->param('pageIndex', 1);
        $intPageSize = $request->param('pageSize', 50);
        $arrData = $ProductService->getInventoryList($param, (int)$intPage, (int)$intPageSize);
        return json($arrData);
    }

    public function discount(Request $request, ProductValidate $validate, ProductService $ProductService)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids', []);
        $arrData = $ProductService->discount($param);
        return json($arrData);
    }

    private function formatParam($fix, $param, $type)
    {
        $groupedParams = [];

        // 定义前缀的正则表达式
        $pattern = '/^' . $fix . '(\d+)-(\d+)$/';

        // 遍历所有参数，筛选并提取符合条件的参数
        foreach ($param as $key => $value) {
            if (preg_match($pattern, $key, $matches)) {
                // 提取 {param1} 和 {param2} 的值
                $param1 = $matches[1];
                $param2 = $matches[2];

                // 按第二个参数分组
                if (!isset($groupedParams[$param2])) {
                    $groupedParams[$param2] = [];
                }

                // 存储提取出的参数值
                $groupedParams[$param2][$key] = [
                    'param1' => $param1,
                    'param2' => $param2,
                    'value' => $value,
                    'type' => $type
                ];
            }
        }
        return $groupedParams;
    }

    private function mergeMultipleArrays(...$arrays)
    {
        $merged = [];

        foreach ($arrays as $array) {
            foreach ($array as $key => $value) {
                if (!isset($merged[$key])) {
                    $merged[$key] = [];
                }
                $merged[$key] = array_merge($merged[$key], $value);
            }
        }

        return $merged;
    }

    private function groupBySalesPriceParam($fix, $params)
    {
        $grouped = [];

        foreach ($params as $key => $value) {
            if (strpos($key, $fix . '-') === 0) {
                // 提取 param 部分
                $param = explode('-', $key)[1];
                if (!isset($grouped[$param])) {
                    $grouped[$param] = [];
                }
                $grouped[$param][$fix] = $value;
            }
        }
        return $grouped;
    }

    public function getFieldEnum(Request $request, ProductService $productService)
    {
        $param = $request->param('field', 'ALL');
        $arrData = $productService->getFieldEnum($param);
        return json($arrData);
    }

    /**
     *  获取枚举资源
     */
    public function resource(Request $request, ProductValidate $validate, ProductService $ProductService)
    {
        $param['field'] = $request->param('field', null);

        if (!$validate->scene('resource')->check($param)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $result = $ProductService->getEnum($param);
        return Basic::json($result);
    }
}
